package Utils;





import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;


public class HibernateExecuter {

	//private SessionFactory sessionFactory = null;
	
	public HibernateExecuter(){
	//setSessionFactory(HibernateUtil.getSessionFactory());
	}
	
	
	public Object executeQuery(IHibernateHQLQuery hibernateHQLQuery) throws Exception
	{
		Object object = null;
		//SessionFactory sessionFactory = HibernateUtil.getInstance().getSessionFactory();
		//SessionFactory sessionFactory = getSessionFactory();
        
        if(hibernateHQLQuery!=null )
        {
        	Session session = null;
        	Transaction tx = null;
        	
            try
            {
                session = HibernateUtil.getSessionFactory().openSession();
                
               
                
                tx = session.beginTransaction();
                
                object = hibernateHQLQuery.executeQuery(session);
                
                tx.commit();
                
                
            }
            catch(Exception e)
            {
            	System.out.println(e.getMessage());
    			if (tx != null) 
    			{
    				try {
    					tx.rollback();
    				} 
    				catch (HibernateException ex) 
    				{
    					System.out.println(ex.getMessage());
    				}
    			}
    			
    			throw e;
            }
           
            finally
            {
            	if(session!=null)
            	{
            		try
            		{
            			session.close();
            		}
            		catch (HibernateException e) 
            		{
            			System.out.println(e.getMessage());
            		}
            	}
            }
        }
		
		return object;	
	
}



	
	
}
